package com.itheima.stringdemo;

import java.util.Scanner;

public class stringDemo8 {
    public static void main(String[] args) {
        //1.键盘录入一个金额
        Scanner sc = new Scanner(System.in);
        int money;//定义
        while (true) {
            System.out.println("请输入一个金额：");
            money = sc.nextInt();//赋值
            if(money>=0 && money<=9999999){
                break;
            }else{
                System.out.println("数据非法");
            }
        }

        //System.out.println(money);

        //定义一个变量表示钱的大写
        String moneyStr = "";
        //得到money中的每一位数字
        while(true){
            //从右往左获取数据
            int ge = money%10;
            String capitalNumber = getCapitalNumber(ge);
            //转换之后的大写拼接到钱的大写中
            moneyStr = capitalNumber+moneyStr;//注意顺序要反过来
            //System.out.println(capitalNumber);
            money/=10;
            if(money==0){
                break;
            }
        }
        //System.out.println(moneyStr);

        //3.在前面补0,补齐七位
        int count = 7-moneyStr.length();
        for (int i = 0; i < count; i++) {
            moneyStr = "零"+moneyStr;
        }
        //System.out.println(moneyStr);

        //4.插入单位
        String[] arr = {"佰","拾","万","仟","佰","拾","元"};
        String result = "";
        //遍历moneyStr，依次得到每一位数字，然后跟单位进行拼接
        for (int i = 0; i < moneyStr.length(); i++) {
            char c = moneyStr.charAt(i);
            //System.out.print(c);//打印数字
            //System.out.print(arr[i]);//打印单位
            result = result +c + arr[i];
        }

        System.out.println(result);

    }

    //定义一个方法把数字变成大写的中文
    public static String getCapitalNumber(int number){
        //定义一个数组，让数字和大写的中文产生对应关系
        String[] arr = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
        //返回结果
        return arr[number];
    }
}
